from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect
from django.core.urlresolvers import reverse
from apps.blog.models import Tag, Article
from apps.blog.forms import TagForm


def index(request):
    all_article = Article.objects.all()
    content = {'post_list': all_article}
    print(content)
    return render(request, 'blog/index.html', content)


def dateil(request, pks):
    posts_article = Article.objects.get(pk=pks)
    print(posts_article)
    content = {'posts': posts_article}
    return render(request, 'blog/dateil.html', content)


def newtag(request):
    if request.method == 'POST':
        form = TagForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect(reverse('blog:index'))
    else:
        form = TagForm()
    content = {'form': form}
    return render(request, 'blog/newtag.html', content)
